package com.yang.controller;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yang.domain.Category;
import com.yang.domain.Goods;
import com.yang.domain.User;
import com.yang.handler.UserThreadLocal;
import com.yang.service.GoodsService;
import com.yang.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 货源Controller
 *
 * @author 不是菜鸡爱编程
 * @date 2024-02-18
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "货源模块", value = "货源Controller")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 查询货源列表
     */
    @ApiOperation("查询货源列表")
    @GetMapping("/list")
    public R list(Goods goods, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        MPJLambdaWrapper<Goods> wrapper = new MPJLambdaWrapper<Goods>()
                .selectAll(Goods.class)
                .selectAs(Category::getName, Goods::getCategoryName)
                .leftJoin(Category.class, Category::getId, Goods::getCategoryId)
                .eq(ObjUtil.isNotEmpty(goods.getId()), Goods::getId, goods.getId())
                .eq(ObjUtil.isNotEmpty(goods.getName()), Goods::getName, goods.getName())
                .eq(ObjUtil.isNotEmpty(goods.getAddress()), Goods::getAddress, goods.getAddress())
                .eq(ObjUtil.isNotEmpty(goods.getCategoryId()), Goods::getCategoryId, goods.getCategoryId())
                .eq(ObjUtil.isNotEmpty(goods.getPrice()), Goods::getPrice, goods.getPrice())
                .eq(ObjUtil.isNotEmpty(goods.getDelFlag()), Goods::getDelFlag, goods.getDelFlag())
                .groupBy(Goods::getId);
        User user = UserThreadLocal.get();
        Integer isAdmin = user.getIsAdmin();
        // 普通用户只能查看未删除的数据
        if (isAdmin != 0) {
            wrapper.eq(Goods::getDelFlag, 0);
        }
        // 货主只能查看自己发布的货源
        wrapper.eq(isAdmin == 2, Goods::getCreateBy, user.getId());
        IPage<Map<String, Object>> page = goodsService.selectJoinMapsPage(new Page<>(pageNum, pageSize), wrapper);
        return R.ok().setData(page);
    }


    /**
     * 获取货源详细信息
     */
    @ApiOperation("获取货源详细信息")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        return R.ok().setData(goodsService.getById(id));
    }

    /**
     * 新增货源
     */
    @ApiOperation("新增货源")
    @PostMapping
    public R add(@RequestBody Goods goods) {
        User user = UserThreadLocal.get();
        goods.setCreateBy(user.getId());
        return R.ok().setData(goodsService.save(goods));
    }

    /**
     * 修改货源
     */
    @ApiOperation("修改货源")
    @PutMapping
    public R edit(@RequestBody Goods goods) {
        return R.ok().setData(goodsService.update(goods, new LambdaQueryWrapper<Goods>()
                .eq(Goods::getId, goods.getId())));
    }

    /**
     * 删除货源
     */
    @ApiOperation("删除货源")
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        User user = UserThreadLocal.get();
        return R.ok().setData(goodsService.update(null, new LambdaUpdateWrapper<Goods>()
                .set(Goods::getDelFlag, 1)
                .in(Goods::getId, Arrays.asList(ids))));
    }
}
